/*
 * SetDialog.java
 *
 * Created on 2010-8-10, 20:52:33
 */

package com.mkk.deskTopSearch.view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;

import com.mkk.deskTopSearch.util.SystemUtil;
import com.mkk.deskTopSearch.worker.CreateIndexWorker;

/**
 * 设置搜索
 * 
 * @author mkk
 */
public class SetDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;

	/** Creates new form SetDialog */
	public SetDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		dialog = this;
		initComponents();
		this.initDatas();
		this.processEvents();
	}

	/**
	 * 初始化数据
	 */
	private void initDatas() {
		this.setTitle("设置");
		this.setIconImage(SystemUtil.getSystemLogoIcon().getImage());
		this.jFileChooser = new JFileChooser();
		this.jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		// 不可用
		this.jButton2.setVisible(false);
		this.jCheckBox1.setVisible(false);
		this.jLabel3.setText(null);
		this.jLabel2.setVisible(false);
		this.jTextArea1.setToolTipText("索引创建输出信息");

		this.setInfo("选择创建索引的目录", Color.BLUE);
	}

	/**
	 * 处理事件
	 */
	private void processEvents() {
		// 浏览目录事件
		this.jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int res = jFileChooser.showOpenDialog(dialog);
				if (res == JFileChooser.APPROVE_OPTION) {
					file = jFileChooser.getSelectedFile();
					jTextField1.setText(file.getAbsolutePath());
					jButton2.setVisible(true);
					jCheckBox1.setVisible(true);
					jLabel2.setVisible(true);
					setInfo("点击 '创建索引' 按钮对该目录创建索引", Color.BLUE);
				}
			}
		});
		// 创建索引事件
		this.jButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					jButton2.setVisible(false);
					jCheckBox1.setVisible(false);
					setInfo("创建索引,请稍候...", Color.BLUE);
					boolean flag = false;
					// 处理索引文件是否存在
					if (SystemUtil.isIndexFileExist()) {
						flag = jCheckBox1.isSelected();
					} else {
						flag = true;
					}
					CreateIndexWorker worker = new CreateIndexWorker(
							jTextArea1, file, flag);
					worker.execute();
					setInfo(worker.get(), Color.BLUE);
				} catch (Exception ex) {
					setInfo("创建索引异常!.", Color.RED);
					ex.printStackTrace();
				}
			}
		});
	}

	/**
	 * 设置状态栏信息
	 * 
	 * @param info
	 * @param c
	 */
	private void setInfo(String info, Color c) {
		this.jLabel3.setText(info);
		this.jLabel3.setForeground(c);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {

		jTabbedPane1 = new javax.swing.JTabbedPane();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jCheckBox1 = new javax.swing.JCheckBox();
		jLabel2 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jLabel3 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("首选项");

		jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(
				153, 153, 153), 1, true));
		jPanel1.setToolTipText("创建索引");

		jLabel1.setText("选择要建立索引的目录:");

		jTextField1.setBackground(new java.awt.Color(255, 255, 255));

		jButton1.setText("浏览...");
		jButton1.setToolTipText("浏览...");

		jButton2.setText("创建索引");
		jButton2.setToolTipText("创建索引");

		jCheckBox1.setText("是否覆盖已存在的索引文件");

		jTextArea1.setBackground(new java.awt.Color(255, 255, 255));
		jTextArea1.setColumns(20);
		jTextArea1.setEditable(false);
		jTextArea1.setRows(5);
		jTextArea1.setToolTipText("");
		jScrollPane1.setViewportView(jTextArea1);

		jLabel3.setText("状态栏");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																508,
																Short.MAX_VALUE)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel1)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																						.addGroup(
																								jPanel1Layout
																										.createSequentialGroup()
																										.addComponent(
																												jCheckBox1)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE)
																										.addComponent(
																												jButton2))
																						.addGroup(
																								jPanel1Layout
																										.createSequentialGroup()
																										.addComponent(
																												jTextField1,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												279,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																										.addComponent(
																												jButton1))))
														.addComponent(jLabel2)
														.addComponent(
																jLabel3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																331,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(39, 39, 39)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																jTextField1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jButton1))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jButton2)
														.addComponent(
																jCheckBox1))
										.addGap(33, 33, 33)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel2)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																212,
																Short.MAX_VALUE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel3)
										.addContainerGap()));

		jTabbedPane1.addTab("创建索引", jPanel1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 535,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				SetDialog dialog = new SetDialog(new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JCheckBox jCheckBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTabbedPane jTabbedPane1;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextField jTextField1;
	// End of variables declaration//GEN-END:variables
	private JFileChooser jFileChooser;
	private SetDialog dialog;
	// 选择的要创建索引的目录
	private File file;

}
